package priv.happy.mealpickpro.log;

import cn.hutool.core.date.DateUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import priv.happy.mealpickpro.domain.PickLog;
import priv.happy.mealpickpro.dto.FoodDTO;
import priv.happy.mealpickpro.service.PickLogService;
import priv.happy.mealpickpro.vo.FoodVO;

/**
 * @Author: HAPPY
 * @Project_name: mealPick
 * @Package_name: priv.happy.mealpick.log
 * @Date: 2023/10/11 8:45
 * @Description:
 */
@Aspect
@Component
public class PickLogAspect {
    @Autowired
    PickLogService pickLogService;

    @Around(value = "execution(* priv.happy.mealpickpro.service.impl.FoodServiceImpl.pickFood(..))")
    public Object pickLog(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        FoodVO result = null;
        try {
            // 获取执行参数
            Object[] args = proceedingJoinPoint.getArgs();
            FoodDTO foodDTO = (FoodDTO) args[0];
            // 得到执行结果，是一个FoodVO
            result = (FoodVO) proceedingJoinPoint.proceed();
            if (result == null) {
                pickLogService.save(new PickLog().setPickTime(DateUtil.now()).setUserId(foodDTO.getUserId()).setFoodName("未找匹配到合适的食物"));
            } else {
                pickLogService.save(new PickLog().setPickTime(DateUtil.now()).setUserId(foodDTO.getUserId()).setFoodId(result.getId()).setFoodName(result.getName()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}
